package com.shijun.im.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.shijun.im.activity.BaseFragmentActivity;

/**
 * Created by zhushijun on 2017/9/23 0023.
 */

public abstract class BaseFragment extends Fragment {
    protected Button backButton, rightButton;
    protected TextView titleTextView;

    protected void initView() {
        backButton = ((BaseFragmentActivity) getActivity()).getBackButton();
        rightButton = ((BaseFragmentActivity) getActivity()).getRightButton();
        titleTextView = ((BaseFragmentActivity) getActivity()).getTitleTextView();
    }

    protected abstract void setTitleView();

    @Override
    public void onResume() {
        super.onResume();
        initView();
        setTitleView();
    }

    @Override
    public void onPause() {
        super.onPause();
        closeSoftKeyboard();
    }


    protected void closeSoftKeyboard() {
        View view = getActivity().getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

        }
    }

    /**
     * @param str  吐司信息
     * @param time 持续时间（0为3秒 1为5秒）
     */
    public void ShowToast(String str, int time) {
        Toast.makeText(getActivity(), str, time).show();
    }
}
